package com.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CitySearchServlet
 */
@WebServlet("/citySearch")
public class CitySearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CitySearchServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String countryCode = request.getParameter("countryCode");

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.print("<html><body><h1>");
		try {
			List<String> cities = new SearchDAO()
					.SearchByCountryCode(countryCode);

			if (cities.size() == 0) {
				out.print("There are no Cities with Given Country Code");
			} else {
				out.print("Total Cites available in given Country code "
						+ countryCode + "   is " + cities.size());

			}

			out.print("</h1></body></html>");

			out.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
